/*
   Copyright (C) 2006
   Andrew Madigan

This file is part of nhttpd.

nhttpd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

nhttpd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with nhttpd; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

//Execution code and setup (Threads and Processes)
#include <stdbool.h>
#include <nhttpd/object.h>
#include <nhttpd/list.h>
#include <nhttpd/request.h>
#include <nhttpd/threadpool.h>
#include "nhconfig.h"

typedef struct {
  Object super;
  /*
	 -1 if never forked
	  0 if we are the parent
	  1 if we are the child
	*/
  int child;

  //User and Group ID to run under
  uid_t uid;
  gid_t gid;
  
  ThreadPool* pool;
  ThreadPool* connectionPool;

  /*
	 If we are the subprocess, this socket connects to the parent. If we are the
	 parent, this socket talks to this subprocess.
	*/
  int socket;
  
  /*
	 Applications that this process will be running.
	*/
  List*/*<Application*>*/ applications;

  /*
	 Connectors that this process will be handling incoming connections on, this
	 is ONLY used if this process is the ONLY process processing requests on the
	 given connection. A seperate facility is used for request routing from the
	 parent process to subprocesses.
	*/
  List*/*<Connector*>*/ connectors;

  /*
	 This is only used on the parent, it tells the server whether to route a
	 request to this process.
	*/
  List*/*<Mapping*>*/ mappings;
  
  List*/*<NH_Process*>*/ children;
} NH_Process;

/*
Initialize anything in the library that is needed. Note that this calls
detect_init() for you.
*/
void NH_init();

/*
De-initialize anything in the library that is needed.
*/
void NH_exit();

/*
  Creates a new process struct with the given uid and gid, all collections are
  initialized. Note that it is up to the caller to set up applications.
  
  self->socket and self->child will be -1
 */
NH_Process* NP_Create(Configuration* config);

/*
  Forks a process based on the properties of this struct. self->socket
  will be defined (in both the parent and child) after this function.
 */
bool NP_Fork(NH_Process* self);

/*
  Start all needed threads for receiving and processing requests now.
 */
void NP_Run(NH_Process* self);

/*
	Stop all processes and threads, currently served requests will be completed
	first. To stop all threads and processes immediately, use HardStop.
*/
void NP_SoftStop(NH_Process* self);

/*
	Stop all processes and threads ASAP.
*/
void NP_HardStop(NH_Process* self);

/*
  Process this request. If we are the parent of the process that needs to
  process this request, the request will be sent to the correct process.
  If we are the correct process, the request will be sent to a thread.
 */
void NP_Route(NH_Process* self, HTTP_Request* request, Application* app);
